#include "bsp.h"

flex_button_t user_button[USER_BUTTON_MAX];

SemaphoreHandle_t xUartSemaphore;

static uint8_t common_btn_read(void *arg)
{
  uint8_t value = 0;

  flex_button_t *btn = (flex_button_t *)arg;

  switch (btn->id)
  {
  case USER_BUTTON_0:
    value = gpio_input_data_bit_read(GPIOA, GPIO_PINS_3);
    break;
  case USER_BUTTON_1:
    value =	gpio_input_data_bit_read(GPIOA, GPIO_PINS_4);
    break;
  case USER_BUTTON_2:
    value = gpio_input_data_bit_read(GPIOB, GPIO_PINS_0);
    break;
  case USER_BUTTON_3:
    value = gpio_input_data_bit_read(GPIOB, GPIO_PINS_11);
    break;
  }

  return value;
}

static void common_btn_evt_cb(void *arg)
{
  flex_button_t *btn = (flex_button_t *)arg;

//	printf("id: [%d]  event: [%d]  repeat: %d\r\n",
//			btn->id,
//			btn->event,
//			btn->click_cnt);

  if(btn->id == USER_BUTTON_0 && btn->event == FLEX_BTN_PRESS_CLICK)
  {
    menu_up();
  }
  if(btn->id == USER_BUTTON_1 && btn->event == FLEX_BTN_PRESS_CLICK)
  {
    menu_down();
  }
  if(btn->id == USER_BUTTON_0 && btn->event == FLEX_BTN_PRESS_DOUBLE_CLICK)
  {
    menu_back();
  }
  if(btn->id == USER_BUTTON_1 && btn->event == FLEX_BTN_PRESS_DOUBLE_CLICK)
  {
    menu_go();
  }
}

void user_button_init(void)
{
  gpio_init_type gpio_initstruct;
  gpio_default_para_init(&gpio_initstruct);

  crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
  crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);

  gpio_initstruct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  gpio_initstruct.gpio_mode = GPIO_MODE_INPUT;
  gpio_initstruct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  gpio_initstruct.gpio_pins = GPIO_PINS_3 | GPIO_PINS_4;
  gpio_initstruct.gpio_pull = GPIO_PULL_UP;
  gpio_init(GPIOA, &gpio_initstruct);

  gpio_initstruct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  gpio_initstruct.gpio_mode = GPIO_MODE_INPUT;
  gpio_initstruct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  gpio_initstruct.gpio_pins = GPIO_PINS_0 | GPIO_PINS_11;
  gpio_initstruct.gpio_pull = GPIO_PULL_UP;
  gpio_init(GPIOB, &gpio_initstruct);

  for (uint8_t i = 0; i < USER_BUTTON_MAX; i++)
  {
    user_button[i].id = i;
    user_button[i].usr_button_read = common_btn_read;
    user_button[i].cb = common_btn_evt_cb;
    user_button[i].pressed_logic_level = 0;
    user_button[i].short_press_start_tick = FLEX_MS_TO_SCAN_CNT(1500);
    user_button[i].long_press_start_tick = FLEX_MS_TO_SCAN_CNT(3000);
    user_button[i].long_hold_start_tick = FLEX_MS_TO_SCAN_CNT(4500);

    flex_button_register(&user_button[i]);
  }
}

void button_scan_task(void *pvParameters)
{
  user_button_init();
  while(1)
  {
    flex_button_scan();
    vTaskDelay(pdMS_TO_TICKS(20));
  }
  vTaskDelete(NULL);
}

void debug_printf_init(void)
{
  uart_print_init(115200);
  xUartSemaphore = xSemaphoreCreateMutex();
}

int debug_printf(char *format, ...)
{
  int ret = 0;
  if(xSemaphoreTake( xUartSemaphore, portMAX_DELAY) == pdTRUE )
  {
    static char buffer[80];
    va_list aptr;

    va_start(aptr, format);
    ret = vsprintf(buffer, format, aptr);
    va_end(aptr);
    printf("%s", buffer);
    xSemaphoreGive( xUartSemaphore );
  }

  return ret;
}